package metrics

import (
	"context"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Prometheus的数据类型有四种
// Counter：只增不减的计数器
// Gauge：可增可减的仪表盘
// Histogram 直方图 数据分布情况
// Summary 摘要 数据分布情况
// Vec代表分区
var (
	// 定义 Histogram 用于记录请求耗时
	HttpRequestDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "request_time_histogram",
			Help:    "Histogram of HTTP request durations in seconds",
			Buckets: prometheus.DefBuckets, // 默认分桶
		},
		[]string{"method", "path", "userId", "status"},
	)
	//Summary类型
	HttpRequestDuration2 = prometheus.NewSummaryVec(
		prometheus.SummaryOpts{
			Name:       "request_time_summary",
			Help:       "just test",
			Objectives: map[float64]float64{0.5: 0.01, 0.9: 0.01, 0.99: 0.001},
		},
		[]string{"method", "path", "userId", "status"},
	)
	// 总请求次数
	HttpRequestCounts = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "qps_statistic",
			Help: "Total number of requests",
		},
		[]string{"method", "path", "status"},
	)
	// 请求短剧次数
	HttpShortDramaCounts = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "request_short_drama_total",
			Help: "Total number of order requests",
		},
		[]string{"userId", "shortDramaId"},
	)
	// 请求下单解锁短剧次数
	HttpUnlockShortDramaCounts = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "request_unlock_short_drama_total",
			Help: "Total number of order requests",
		},
		[]string{"userId", "shortDramaId"},
	)
	// 用户下单次数（充值）
	HttpOrderCounts = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "request_order_total",
			Help: "Total number of order requests",
		},
		[]string{"userId", "goodsId"},
	)
	// 用户登录次数
	UserLoginAttempts = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "user_login_attempts_total",              // 指标名称
			Help: "Total number of login attempts by user", // 指标说明
		},
		[]string{"userId"}, // 使用用户名作为标签
	)
)

// Init 注册指标
func Init(ctx context.Context) {
	// 注册指标到 Prometheus 的默认注册器
	prometheus.MustRegister(HttpRequestDuration)
	prometheus.MustRegister(HttpRequestDuration2)
	prometheus.MustRegister(HttpRequestCounts)          //所有接口请求次数
	prometheus.MustRegister(HttpUnlockShortDramaCounts) //用户解锁请求次数
	prometheus.MustRegister(HttpShortDramaCounts)       //用户商品请求次数
	prometheus.MustRegister(HttpOrderCounts)            //用户下单请求次数
	prometheus.MustRegister(UserLoginAttempts)
}

// PrometheusHandler 返回 Prometheus 的 HTTP Handler
func PrometheusHandler() http.Handler {
	return promhttp.Handler()
}

func RequestDuration() {

}
